package com.conn3cted.phrasebook.web.mobile;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.conn3cted.phrasebook.domain.Person;
import com.conn3cted.phrasebook.domain.Phrase;
import com.conn3cted.phrasebook.web.BaseController;

@Controller
@RequestMapping(value = "/m/people")
public class MobilePeopleController extends BaseController {
	


	public List<Phrase> getPhrases(Person p){
		List<Phrase> phrases = new ArrayList<Phrase>( p.getPhrases());
		sortByVotes(phrases);
		return phrases;
	}
	
	@RequestMapping(value = "list", method = RequestMethod.GET)
	public String listPeople(Model uiModel) {
		
		if (contextModel.getPerson() == null) {
			return "mobile/login";
		}

		List<Person> people = Person.findAllPeople();
		uiModel.addAttribute("people", people);
		
		return "mobile/list";
	}
	
	
	@RequestMapping(value = "detail", method = RequestMethod.GET)
	public String detail(@RequestParam("id") long personId, Model uiModel) {

		if (contextModel.getPerson() == null) {
			return "mobile/login";
		}
		
		Person p = Person.findPerson(personId);
		uiModel.addAttribute("person", p);
		uiModel.addAttribute("phrases", getPhrases(p));
		return  "mobile/detail";	
	}
	
	
	@RequestMapping(value = "addPhrase" , method = RequestMethod.POST)
	public String onSubmit(@RequestParam("id") long personId, @RequestParam("phrase") String phrase, Model uiModel) {
				
		Person p = Person.findPerson(personId);
		
		//create phrase
		Phrase newPhrase = new Phrase();
		newPhrase.setTitle(phrase);
		newPhrase.setVotes(0);
		
		p.getPhrases().add(newPhrase);
		
		p.merge();

		uiModel.addAttribute("person", p);
		uiModel.addAttribute("phrases", getPhrases(p));
		return "mobile/detail";

	}
	
	@RequestMapping(value = "voteUp", method = RequestMethod.GET)
	public String voteUp(@RequestParam("id") long phraseId,@RequestParam("pId") long personId, Model uiModel) {
		Person p = Person.findPerson(personId);
		uiModel.addAttribute("person", p);
		
		
		Phrase phrase = Phrase.findPhrase(phraseId);
		if (phrase != null) {
			phrase.setVotes(phrase.getVotes() + 1 );
			phrase.merge();
		}
		
		uiModel.addAttribute("phrases", getPhrases(p));
		return "mobile/detail";
	}
	
	
	private List<Phrase> sortByVotes(List<Phrase> toSort) {
		Comparator<Phrase> comp = new Comparator<Phrase>() {
			
			@Override
			public int compare(Phrase o1, Phrase o2) {
				if (o1.getVotes() > o2.getVotes())
					return -1;
				if (o1.getVotes() < o2.getVotes())
					return 1;
				
				if (o1.getVotes() > 0 && o2.getVotes() > 0 && o1.getVotes() == o2.getVotes()) // both have been voted on and have equal votes so equal
					return 0;

				//now sort on creation date if both have no votes
				if (o1.getCreated().getTime() > o2.getCreated().getTime() )
					return 1;
				if (o1.getCreated().getTime() < o2.getCreated().getTime() )
					return -1;
				return 0;
	}
		};
	
		Collections.sort(toSort, comp);
		return toSort;
	}
}
